package org.jeecg.modules.engineering.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.engineering.entity.EquipDataCollector;
import org.jeecg.modules.engineering.entity.EquipDataCollectorCode;
import org.jeecg.modules.engineering.mapper.EquipDataCollectorCodeMapper;
import org.jeecg.modules.engineering.mapper.EquipDataCollectorMapper;
import org.jeecg.modules.engineering.service.IEquipDataCollectorService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 设备数据采集商表
 * @Author: jeecg-boot
 * @Date:   2019-08-27
 * @Version: V1.0
 */
@Service
public class EquipDataCollectorServiceImpl extends ServiceImpl<EquipDataCollectorMapper, EquipDataCollector> implements IEquipDataCollectorService {

	@Autowired
	private EquipDataCollectorMapper equipDataCollectorMapper;
	@Autowired
	private EquipDataCollectorCodeMapper equipDataCollectorCodeMapper;
	
	@Override
	@Transactional
	public void saveMain(EquipDataCollector equipDataCollector, List<EquipDataCollectorCode> equipDataCollectorCodeList) {
		equipDataCollectorMapper.insert(equipDataCollector);
		for(EquipDataCollectorCode entity:equipDataCollectorCodeList) {
			//外键设置
			entity.setCollectorId(equipDataCollector.getId());
			entity.setId(UUIDGenerator.getUUID());
			equipDataCollectorCodeMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(EquipDataCollector equipDataCollector,List<EquipDataCollectorCode> equipDataCollectorCodeList) {
		equipDataCollectorMapper.updateById(equipDataCollector);
		
		//1.先删除子表数据
		equipDataCollectorCodeMapper.deleteByMainId(equipDataCollector.getId());
		
		//2.子表数据重新插入
		for(EquipDataCollectorCode entity:equipDataCollectorCodeList) {
			//外键设置
			entity.setCollectorId(equipDataCollector.getId());
			entity.setId(UUIDGenerator.getUUID());
			equipDataCollectorCodeMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		equipDataCollectorCodeMapper.deleteByMainId(id);
		equipDataCollectorMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			equipDataCollectorCodeMapper.deleteByMainId(id.toString());
			equipDataCollectorMapper.deleteById(id);
		}
	}

	@Override
	public List<EquipDataCollector> selectAll(boolean isStandard) {
		return equipDataCollectorMapper.selectAll(isStandard);
	}

	@Override
	public IPage<EquipDataCollector> selectSqlList(Page<EquipDataCollector> page, String collectorName, String collectorCode) {
		return page.setRecords(equipDataCollectorMapper.selectSqlList(page,collectorName,collectorCode));
	}

}
